<?php
/**
 * Created by PhpStorm.
 * User: nirui
 * Date: 2016-09-14
 * Time: 16:03
 */
class Controller_checkstandard extends Template
{
    /**
     *质检标准确认
     */
    public function before()
    {
        try{
            ACL::Required('check quality standard');
        }catch(HTTP_Exception_403 $e){
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect(URL::base(TRUE));
        }
        parent::before();
    }

    public function action_index(){
        $this->title = "SKU质检标准确认";

        $batch_code = isset($_GET['batch_code']) ? trim($_GET['batch_code']) : '';
        $skuchecksignselected = isset($_GET['skuchecksignselected']) ? $_GET['skuchecksignselected'] : 10;
        $recieve_details_id = isset($_GET['recieve_details_id']) ? addslashes(trim($_GET['recieve_details_id'])) : '';

        $view = View::factory('quality/checksku');
        $view->batch_code = $batch_code;
        $view->skuchecksignselected = $skuchecksignselected;
        $view->recieve_details_id = $recieve_details_id;


        if(!empty($batch_code) || !empty($recieve_details_id)) {
            if(!empty($batch_code)) {
                $sql='SELECT `id` FROM `batches` WHERE `batch_code`='.$batch_code;
                if(DB::query(Database::SELECT,$sql)->execute('newerp')->get('id')){
                    $sql='SELECT * FROM `recieve_details` WHERE `batch_code`="'.$batch_code.'"';
                } else {
                    $flag = false;
                    Message::error('你扫描的批次号不存在！！');
                }
            }

            if(!isset($flag)) {
                !isset($sql) && $sql = 'SELECT * FROM `recieve_details` WHERE 1 ';

                $skuchecksignselected != -1 && $sql .= ' AND `skuchecksign`=' . $skuchecksignselected;
                $recieve_details_id != '' && $sql .= ' AND `id`=' . $recieve_details_id;

                //获得收货明细
                $recieve_details=DB::query(Database::SELECT,$sql)->execute('newerp')->as_array('id');
                if(empty($recieve_details)){
                    Message::error('没有符合条件的数据！');
                } else {
                    $recieve_ids=array();
                    foreach($recieve_details as $recieve_detail_id=>$details){
                        $recieve_ids[$recieve_detail_id]=$details['recieve_invoice_id'];
                    }
                    //查询采购单相关信息
                    $sql='SELECT * FROM `recieve_invoices` WHERE id IN ("'.implode('","',$recieve_ids).'")';
                    $recieve_invoices=DB::query(Database::SELECT,$sql)->execute('newerp')->as_array('id');

                    $recieve_infos=array();

                    foreach($recieve_ids as $recieve_id=>$recieve_info){
                        $sql="SELECT `export_tax_rebate`
                              FROM `purchaseorders`
                              WHERE `id`=".$recieve_invoices[$recieve_ids[$recieve_id]]['purchaseorder_id'];
                        $export_tax_rebate=DB::query(Database::SELECT,$sql)->execute('newerp')->get('export_tax_rebate');
                        $recieve_infos[$recieve_id]['export_tax_rebate']=$export_tax_rebate;
                        $recieve_infos[$recieve_id]['id']=$recieve_id;
                        $sql='SELECT `nick` FROM `users` WHERE `id`='.$recieve_invoices[$recieve_info]['recieve_man'];
                        $recieve_infos[$recieve_id]['recieve_man']=DB::query(Database::SELECT,$sql)->execute()->get('nick');
                        $recieve_infos[$recieve_id]['purchaseorder_id']=$recieve_invoices[$recieve_ids[$recieve_id]]['purchaseorder_id'];
                        $purchaseorder_id=$recieve_infos[$recieve_id]['purchaseorder_id'];
                        $recieve_infos[$recieve_id]['purchase_man']=Purchase::getPurchase_nameByPurchaseorder_id($purchaseorder_id);
                        $recieve_infos[$recieve_id]['sku']=$recieve_details[$recieve_id]['sku'];
                        $recieve_infos[$recieve_id]['sku_name']=$recieve_details[$recieve_id]['sku_name'];
                        $recieve_infos[$recieve_id]['arrival_quantity']=$recieve_details[$recieve_id]['arrival_quantity'];
                        $sql='SELECT `qc_standard_id` FROM `skusystem_sku_qc_standard` WHERE `sku` ="'.$recieve_infos[$recieve_id]['sku'].'"';
                        $skucheckdetail_ids=DB::query(Database::SELECT,$sql)->execute('newerp')->get('qc_standard_id');
                        $sql='SELECT `standard` FROM `skusystem_qc_standard` WHERE `id` IN';
                        empty($skucheckdetail_ids)?$sql.='(0)':$sql.='('.$skucheckdetail_ids.')';
                        $skucheckdetails=DB::query(Database::SELECT,$sql)->execute('newerp')->as_array('standard');
                        $recieve_infos[$recieve_id]['skucheckdetails']=array_keys($skucheckdetails);
                        $recieve_infos[$recieve_id]['skuchecksign']=$recieve_details[$recieve_id]['skuchecksign'];
                        $recieve_infos[$recieve_id]['batch_code']=$recieve_details[$recieve_id]['batch_code'];
                        if($recieve_details[$recieve_id]['check_man'] != 0) {
                            $sql='SELECT `nick` FROM `users` WHERE `id`='.$recieve_details[$recieve_id]['check_man'];
                            $recieve_infos[$recieve_id]['check_man_name']=DB::query(Database::SELECT,$sql)->execute()->get('nick');
                        }
                        $recieve_infos[$recieve_id]['check_time']=$recieve_details[$recieve_id]['check_time'];
                    }
                    $view->recieve_infos=$recieve_infos;
                }
            }
        } else {
            if(count($_GET)>1){
                Message::error('请输入批次号或者收货明细号！');
            }
        }

        $this->response->body($view);
    }

    public function Action_updateskuchecksign(){
        $sql='SELECT `skuchecksign` FROM `recieve_details` WHERE id = ' . addslashes(trim($_GET['searchDetailId']));
        $sign=DB::query(Database::SELECT,$sql)->execute('newerp')->get('skuchecksign');
        if($sign==20){
            Message::error('SKU质检标准已确认！请勿重复点击！');
        }else{
            $id=User::active_user()->id;
            $time=date('Y-m-d H:i:s');
            $sql='UPDATE `recieve_details` SET `skuchecksign`=20,`check_man`='.$id.',`check_time`="'.$time.'" WHERE id=' . addslashes(trim($_GET['searchDetailId']));
            $sign=DB::query(Database::UPDATE,$sql)->execute('newerp');
            if($sign){
                Message::success('质检确认成功！');
            }else{
                Message::error('质检确认失败！');
            }
        }

        $this->request->redirect('checkstandard/index?recieve_details_id=' .$_GET['recieve_details_id'] .
            '&batch_code=' . $_GET['batch_code'] . '&skuchecksignselected=' . $_GET['skuchecksignselected']
        );
    }

    public function Action_checkStandardMore(){
        $id=User::active_user()->id;
        $time=date('Y-m-d H:i:s');
        $sql='UPDATE `recieve_details` SET `skuchecksign`=20,`check_man`='.$id.',`check_time`="'.$time.'" WHERE id IN('.join(',',$_POST['receiptDetailIds']).')';
        DB::query(Database::UPDATE,$sql)->execute('newerp');
        echo sizeof($_POST['receiptDetailIds']);exit;
    }
}